﻿using System;

public class Mapper015 : MapperDefault
{
    public override void init(NES nes)
    {
        base.init(nes);
    }

    public override void loadROM(ROM rom)
    {
        if (!rom.Valid)
        {
            Console.Out.WriteLine("015: Invalid ROM! Unable to load.");
        }
        else
        {
            this.load8kRomBank(0, 0x8000);
            this.load8kRomBank(1, 0xa000);
            this.load8kRomBank(2, 0xc000);
            this.load8kRomBank(3, 0xe000);
            this.loadCHRROM();
            this.loadBatteryRam();
            base.nes.Cpu.requestIrq(2);
        }
    }

    public override void write(int address, short value_Renamed)
    {
        if (address < 0x8000)
        {
            base.write(address, value_Renamed);
        }
        else
        {
            switch (address)
            {
                case 0x8000:
                    if ((value_Renamed & 0x80) == 0)
                    {
                        this.load8kRomBank((value_Renamed & 0x3f) * 2, 0x8000);
                        this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 1, 0xa000);
                        this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 2, 0xc000);
                        this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 3, 0xe000);
                        break;
                    }
                    this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 1, 0x8000);
                    this.load8kRomBank((value_Renamed & 0x3f) * 2, 0xa000);
                    this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 3, 0xc000);
                    this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 2, 0xe000);
                    break;

                case 0x8001:
                    if ((value_Renamed & 0x80) == 0)
                    {
                        this.load8kRomBank((value_Renamed & 0x3f) * 2, 0xc000);
                        this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 1, 0xe000);
                        return;
                    }
                    this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 1, 0xc000);
                    this.load8kRomBank((value_Renamed & 0x3f) * 2, 0xe000);
                    return;

                case 0x8002:
                    if ((value_Renamed & 0x80) == 0)
                    {
                        this.load8kRomBank((value_Renamed & 0x3f) * 2, 0x8000);
                        this.load8kRomBank((value_Renamed & 0x3f) * 2, 0xa000);
                        this.load8kRomBank((value_Renamed & 0x3f) * 2, 0xc000);
                        this.load8kRomBank((value_Renamed & 0x3f) * 2, 0xe000);
                        return;
                    }
                    this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 1, 0x8000);
                    this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 1, 0xa000);
                    this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 1, 0xc000);
                    this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 1, 0xe000);
                    return;

                case 0x8003:
                    if ((value_Renamed & 0x80) == 0)
                    {
                        this.load8kRomBank((value_Renamed & 0x3f) * 2, 0xc000);
                        this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 1, 0xe000);
                    }
                    else
                    {
                        this.load8kRomBank(((value_Renamed & 0x3f) * 2) + 1, 0xc000);
                        this.load8kRomBank((value_Renamed & 0x3f) * 2, 0xe000);
                    }
                    if ((value_Renamed & 0x40) != 0)
                    {
                        base.nes.Ppu.Mirroring = 1;
                    }
                    else
                    {
                        base.nes.Ppu.Mirroring = 0;
                    }
                    return;

                default:
                    return;
            }
            if ((value_Renamed & 0x40) != 0)
            {
                base.nes.Ppu.Mirroring = 1;
            }
            else
            {
                base.nes.Ppu.Mirroring = 0;
            }
        }
    }
}

